﻿using System;
using System.Collections.Generic;
using System.IO;
using Manager;
using UniRx;
using UnityEngine;

namespace Studio
{
	// Token: 0x0200045D RID: 1117
	public class VoiceCtrl
	{
		// Token: 0x170002CB RID: 715
		// (get) Token: 0x06001BDF RID: 7135 RVA: 0x000B2EC8 File Offset: 0x000B10C8
		// (set) Token: 0x06001BE0 RID: 7136 RVA: 0x000B2ED0 File Offset: 0x000B10D0
		public OCIChar ociChar { get; set; }

		// Token: 0x170002CC RID: 716
		// (get) Token: 0x06001BE1 RID: 7137 RVA: 0x000B2EDC File Offset: 0x000B10DC
		// (set) Token: 0x06001BE2 RID: 7138 RVA: 0x000B2EE4 File Offset: 0x000B10E4
		public Transform transVoice { get; private set; }

		// Token: 0x170002CD RID: 717
		// (get) Token: 0x06001BE3 RID: 7139 RVA: 0x000B2EF0 File Offset: 0x000B10F0
		public bool isPlay
		{
			get
			{
				return this.isLoading || (this.source != null && this.source.isPlaying);
			}
		}

		// Token: 0x170002CE RID: 718
		// (get) Token: 0x06001BE4 RID: 7140 RVA: 0x000B2F28 File Offset: 0x000B1128
		private int personality
		{
			get
			{
				return 0;
			}
		}

		// Token: 0x170002CF RID: 719
		// (get) Token: 0x06001BE5 RID: 7141 RVA: 0x000B2F2C File Offset: 0x000B112C
		private float pitch
		{
			get
			{
				return (this.ociChar == null) ? 1f : this.ociChar.charInfo.voicePitch;
			}
		}

		// Token: 0x170002D0 RID: 720
		// (get) Token: 0x06001BE6 RID: 7142 RVA: 0x000B2F54 File Offset: 0x000B1154
		private Transform transHead
		{
			get
			{
				if (this.m_TransformHead == null)
				{
					GameObject gameObject = (this.ociChar == null) ? null : this.ociChar.charInfo.GetReferenceInfo(CharReference.RefObjKey.HeadParent);
					this.m_TransformHead = ((!(gameObject != null)) ? null : gameObject.transform);
				}
				return this.m_TransformHead;
			}
		}

		// Token: 0x06001BE7 RID: 7143 RVA: 0x000B2FBC File Offset: 0x000B11BC
		public bool Play(int _idx)
		{
			if (!Singleton<Info>.IsInstance())
			{
				return false;
			}
			this.Stop();
			if (this.list.Count == 0)
			{
				return false;
			}
			if (!MathfEx.RangeEqualOn<int>(0, _idx, this.list.Count - 1))
			{
				this.index = -1;
				return false;
			}
			VoiceCtrl.VoiceInfo voiceInfo = this.list[_idx];
			Info.LoadCommonInfo loadInfo = this.GetLoadInfo(voiceInfo.group, voiceInfo.category, voiceInfo.no);
			if (loadInfo == null)
			{
				return false;
			}
			Voice instance = Singleton<Voice>.Instance;
			int personality = this.personality;
			string bundlePath = loadInfo.bundlePath;
			string fileName = loadInfo.fileName;
			float pitch = this.pitch;
			Transform transHead = this.transHead;
			this.transVoice = instance.Play(personality, bundlePath, fileName, pitch, 0f, 0f, true, transHead, Voice.Type.PCM, -1, true, true);
			if (this.transVoice == null)
			{
				return false;
			}
			this.source = this.transVoice.GetComponent<AudioSource>();
			this.ociChar.charInfo.human.lipSync.SetAudioSource(this.source);
			this.index = _idx;
			this.isLoading = true;
			(from s in this.source.ObserveEveryValueChanged((AudioSource s) => s.clip, FrameCountType.Update, false)
			where s != null
			select s).Subscribe(delegate(AudioClip _)
			{
				this.isLoading = false;
			});
			this.voiceEndChecker = this.transVoice.gameObject.AddComponent<VoiceEndChecker>();
			VoiceEndChecker voiceEndChecker = this.voiceEndChecker;
			voiceEndChecker.onEndFunc = (VoiceEndChecker.OnEndFunc)Delegate.Combine(voiceEndChecker.onEndFunc, new VoiceEndChecker.OnEndFunc(this.NextVoicePlay));
			this.ociChar.SetVoice();
			return true;
		}

		// Token: 0x06001BE8 RID: 7144 RVA: 0x000B3184 File Offset: 0x000B1384
		public void Stop()
		{
			if (this.voiceEndChecker != null)
			{
				this.voiceEndChecker.onEndFunc = null;
			}
			if (this.source)
			{
				this.source.Stop();
			}
			this.source = null;
			this.transVoice = null;
			this.ociChar.SetVoice();
		}

		// Token: 0x06001BE9 RID: 7145 RVA: 0x000B31E4 File Offset: 0x000B13E4
		public void Save(BinaryWriter _writer, Version _version)
		{
			int count = this.list.Count;
			_writer.Write(count);
			for (int i = 0; i < count; i++)
			{
				VoiceCtrl.VoiceInfo voiceInfo = this.list[i];
				_writer.Write(voiceInfo.group);
				_writer.Write(voiceInfo.category);
				_writer.Write(voiceInfo.no);
			}
			_writer.Write((int)this.repeat);
		}

		// Token: 0x06001BEA RID: 7146 RVA: 0x000B3254 File Offset: 0x000B1454
		public void Load(BinaryReader _reader, Version _version)
		{
			int num = _reader.ReadInt32();
			for (int i = 0; i < num; i++)
			{
				int group = _reader.ReadInt32();
				int category = _reader.ReadInt32();
				int no = _reader.ReadInt32();
				if (this.GetLoadInfo(group, category, no) != null)
				{
					this.list.Add(new VoiceCtrl.VoiceInfo(group, category, no));
				}
			}
			this.repeat = (VoiceCtrl.Repeat)_reader.ReadInt32();
		}

		// Token: 0x06001BEB RID: 7147 RVA: 0x000B32C4 File Offset: 0x000B14C4
		public void SaveList(string _name)
		{
			string path = UserData.Create("studio/voicelist") + Utility.GetCurrentTime() + ".dat";
			using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
			{
				using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
				{
					binaryWriter.Write("【voice】");
					binaryWriter.Write(_name);
					int count = this.list.Count;
					binaryWriter.Write(count);
					for (int i = 0; i < count; i++)
					{
						VoiceCtrl.VoiceInfo voiceInfo = this.list[i];
						binaryWriter.Write(voiceInfo.group);
						binaryWriter.Write(voiceInfo.category);
						binaryWriter.Write(voiceInfo.no);
					}
				}
			}
		}

		// Token: 0x06001BEC RID: 7148 RVA: 0x000B33AC File Offset: 0x000B15AC
		public bool LoadList(string _path, bool _import = false)
		{
			if (!_import)
			{
				this.list.Clear();
			}
			using (FileStream fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					if (string.Compare(binaryReader.ReadString(), "【voice】") != 0)
					{
						return false;
					}
					binaryReader.ReadString();
					int num = binaryReader.ReadInt32();
					for (int i = 0; i < num; i++)
					{
						int group = binaryReader.ReadInt32();
						int category = binaryReader.ReadInt32();
						int no = binaryReader.ReadInt32();
						if (this.GetLoadInfo(group, category, no) != null)
						{
							this.list.Add(new VoiceCtrl.VoiceInfo(group, category, no));
						}
					}
				}
			}
			return true;
		}

		// Token: 0x06001BED RID: 7149 RVA: 0x000B34A4 File Offset: 0x000B16A4
		public static string LoadListName(string _path)
		{
			string result = string.Empty;
			using (FileStream fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					if (string.Compare(binaryReader.ReadString(), "【voice】") != 0)
					{
						return string.Empty;
					}
					result = binaryReader.ReadString();
				}
			}
			return result;
		}

		// Token: 0x06001BEE RID: 7150 RVA: 0x000B3534 File Offset: 0x000B1734
		public static bool CheckIdentifyingCode(string _path)
		{
			bool result = true;
			using (FileStream fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					if (string.Compare(binaryReader.ReadString(), "【voice】") != 0)
					{
						result = false;
					}
				}
			}
			return result;
		}

		// Token: 0x06001BEF RID: 7151 RVA: 0x000B35AC File Offset: 0x000B17AC
		private Info.LoadCommonInfo GetLoadInfo(int _group, int _category, int _no)
		{
			Dictionary<int, Dictionary<int, Info.LoadCommonInfo>> dictionary = null;
			if (!Singleton<Info>.Instance.dicVoiceLoadInfo.TryGetValue(_group, out dictionary))
			{
				return null;
			}
			Dictionary<int, Info.LoadCommonInfo> dictionary2 = null;
			if (!dictionary.TryGetValue(_category, out dictionary2))
			{
				return null;
			}
			Info.LoadCommonInfo result = null;
			if (!dictionary2.TryGetValue(_no, out result))
			{
				return null;
			}
			return result;
		}

		// Token: 0x06001BF0 RID: 7152 RVA: 0x000B35FC File Offset: 0x000B17FC
		private void NextVoicePlay()
		{
			this.transVoice = null;
			VoiceCtrl.Repeat repeat = this.repeat;
			if (repeat != VoiceCtrl.Repeat.None)
			{
				if (repeat != VoiceCtrl.Repeat.All)
				{
					if (repeat == VoiceCtrl.Repeat.Select)
					{
						this.Play(this.index);
					}
				}
				else if (this.list.Count != 0)
				{
					this.index = (this.index + 1) % this.list.Count;
					this.Play(this.index);
				}
			}
			else
			{
				this.index++;
				this.Play(this.index);
			}
		}

		// Token: 0x04001F29 RID: 7977
		public const string savePath = "studio/voicelist";

		// Token: 0x04001F2A RID: 7978
		public const string saveExtension = ".dat";

		// Token: 0x04001F2B RID: 7979
		public const string saveIdentifyingCode = "【voice】";

		// Token: 0x04001F2C RID: 7980
		public List<VoiceCtrl.VoiceInfo> list = new List<VoiceCtrl.VoiceInfo>();

		// Token: 0x04001F2D RID: 7981
		public VoiceCtrl.Repeat repeat;

		// Token: 0x04001F2E RID: 7982
		public int index = -1;

		// Token: 0x04001F31 RID: 7985
		private AudioSource source;

		// Token: 0x04001F32 RID: 7986
		private bool isLoading;

		// Token: 0x04001F33 RID: 7987
		private Transform m_TransformHead;

		// Token: 0x04001F34 RID: 7988
		private VoiceEndChecker voiceEndChecker;

		// Token: 0x0200045E RID: 1118
		public class VoiceInfo
		{
			// Token: 0x06001BF4 RID: 7156 RVA: 0x000B36C4 File Offset: 0x000B18C4
			public VoiceInfo(int _group, int _category, int _no)
			{
				this.group = _group;
				this.category = _category;
				this.no = _no;
			}

			// Token: 0x170002D1 RID: 721
			// (get) Token: 0x06001BF5 RID: 7157 RVA: 0x000B36E4 File Offset: 0x000B18E4
			// (set) Token: 0x06001BF6 RID: 7158 RVA: 0x000B36EC File Offset: 0x000B18EC
			public int group { get; private set; }

			// Token: 0x170002D2 RID: 722
			// (get) Token: 0x06001BF7 RID: 7159 RVA: 0x000B36F8 File Offset: 0x000B18F8
			// (set) Token: 0x06001BF8 RID: 7160 RVA: 0x000B3700 File Offset: 0x000B1900
			public int category { get; private set; }

			// Token: 0x170002D3 RID: 723
			// (get) Token: 0x06001BF9 RID: 7161 RVA: 0x000B370C File Offset: 0x000B190C
			// (set) Token: 0x06001BFA RID: 7162 RVA: 0x000B3714 File Offset: 0x000B1914
			public int no { get; private set; }
		}

		// Token: 0x0200045F RID: 1119
		public enum Repeat
		{
			// Token: 0x04001F3B RID: 7995
			None,
			// Token: 0x04001F3C RID: 7996
			All,
			// Token: 0x04001F3D RID: 7997
			Select
		}
	}
}
